package cc.rengu.igas.opms.core.service.trans.common;

import cc.rengu.igas.opms.common.enums.RespCodeEnum;
import cc.rengu.igas.opms.core.service.base.ManageService;
import cc.rengu.igas.opms.facade.request.GenRandomStrRequest;
import cc.rengu.igas.opms.facade.response.GenRandomStrResponse;
import cc.rengu.igas.share.core.realize.UserService;
import cc.rengu.igas.share.core.realize.impl.UserServiceImpl;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.service.model.BizResponse;
import cc.rengu.oltp.utility.util.ConvertUtil;
import cc.rengu.oltp.utility.util.RandomUtil;
import cc.rengu.oltp.utility.util.StringUtil;

/**
 * 生成随机数的服务
 * 将生成的随机字符串放到sessionBean中
 * WEB收银台使用，前端使用该随机字符串对用户密码进行加密，在验证密码的时候使用该随机字符串进行验证密码
 * modify by wangcheng 2021-09-13
 */
public class GenRandomStringService extends ManageService {

    @Override
    protected Object bizBeforeCust() throws Exception {
        GenRandomStrRequest genRandomStrRequest = new GenRandomStrRequest();
        ConvertUtil.convertOutput(genRandomStrRequest);
        return genRandomStrRequest;
    }

    @Override
    protected BizResponse bizProcessCust(Object request) throws Exception {

        GenRandomStrRequest genRandomStrRequest = (GenRandomStrRequest) request;

        //校验用户登录状态以及会话是否存在
        UserService userService = new UserServiceImpl();

        //获取随机字符串的长度，若未确认长度则默认32位
        String randomStrLength = "32";
        if (!StringUtil.isEmptyOrNull(genRandomStrRequest.getRandomLength())) {
            randomStrLength = genRandomStrRequest.getRandomLength();
        }

        //生成随机字符串
        String randomStr = genRandomStr(Integer.parseInt(randomStrLength), genRandomStrRequest.getRandomType());
        userService.setUserSessionValue("randomString", randomStr);
        rglog.info("随机字符串:<{}>", randomStr);

        //响应信息
        GenRandomStrResponse genRandomStrResponse = new GenRandomStrResponse();
        genRandomStrResponse.setRandomString(randomStr);
        BizResponse<GenRandomStrResponse> bizResponse = new BizResponse<>();
        bizResponse.setRspSysId(AppParamConstant.IGAS);
        bizResponse.setRespCode(RespCodeEnum.TRANS_SUCCESS.getRespCode());
        bizResponse.setRespDesc(RespCodeEnum.TRANS_SUCCESS.getRespDesc());
        bizResponse.setResult(genRandomStrResponse);
        return bizResponse;
    }

    @Override
    protected void bizAfterCust(Object response) throws Exception {
        BizResponse bizResponse = (BizResponse) response;
        GenRandomStrResponse genRandomStrResponse = (GenRandomStrResponse) bizResponse.getResult();
        ConvertUtil.convertInput(genRandomStrResponse);
    }

    /**
     * 生成随机字符串
     *
     * @param randomStrLength 随机字符串长度
     * @param randomType      随机字符串类型：00表示混合字符 01表示纯数字字符
     * @return 随机字符串
     */
    private String genRandomStr(int randomStrLength, String randomType) {

        rglog.info("生成的随机字符串类型:<{}>", randomType);
        //00表示混合字符
        if ("00".equals(randomType)) {
            return RandomUtil.getRandomStr(randomStrLength);
        }
        //01表示纯数字字符
        else if ("01".equals(randomType)) {
            return RandomUtil.randomNumStr(randomStrLength);
        } else {
            rglog.info("不识别的随机数类型.....");
            throw new BizException(RespCodeEnum.PARAM_ERROR.getRespCode(), RespCodeEnum.PARAM_ERROR.getRespDesc());
        }
    }
}

